package io.github.simplejy.miodemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import io.github.simplejy.miodemo.R;
import io.github.simplejy.miodemo.bean.HistoryParamData;

/**
 * 历史参数列表数据适配器
 */

public class HistoryAdapter extends BaseAdapter {
    private static final int TYPE_DATA_ITEM = 0;
    private static final int TYPE_ITEM = 1;

    private ArrayList<HistoryParamData> historyDatas;
    private LayoutInflater mInflater;
    private OnClickHistoryListener onClickHistoryListener;

    public HistoryAdapter(Context context, ArrayList<HistoryParamData> historyDatas) {
        this.historyDatas = historyDatas;
        mInflater = LayoutInflater.from(context);
    }

    public void setOnClickHistoryListener(OnClickHistoryListener onClickHistoryListener) {
        this.onClickHistoryListener = onClickHistoryListener;
    }

    @Override
    public int getCount() {

        int count = 0;

        if (historyDatas != null) {
            //所有分类中item的总和是ListVIew的Item的总个数
            for (HistoryParamData data : historyDatas) {
                count += data.getItemCount();
            }
        }

        return count;
    }

    @Override
    public Object getItem(int position) {

        if (historyDatas == null || position < 0 || position > getCount()) {
            return null;
        }

        int itemFirstIndex = 0;

        for (HistoryParamData data : historyDatas) {
            int size = data.getItemCount();

            int itemIndex = position - itemFirstIndex;

            if (itemIndex < size) {
                return data.getItem(itemIndex);
            }

            itemFirstIndex += size;
        }

        return null;
    }


    @Override
    public int getItemViewType(int position) {

        if (historyDatas == null || position < 0 || position > getCount()) {
            return TYPE_ITEM;
        }

        int itemFirstIndex = 0;

        for (HistoryParamData data : historyDatas) {
            int size = data.getItemCount();

            int itemIndex = position - itemFirstIndex;

            if (itemIndex == 0) {
                return TYPE_DATA_ITEM;
            }

            itemFirstIndex += size;
        }

        return TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_DATA_ITEM:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_history_list_head, null);
                }
                TextView textView = (TextView) convertView.findViewById(R.id.tv_title);
                String data = (String) getItem(position);
                //设置日期
                textView.setText(data);
                break;
            case TYPE_ITEM:
                ViewHolder hodler;
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_history_param, null);
                    hodler = new ViewHolder();
                    hodler.textView = (TextView) convertView;
                    convertView.setTag(R.id.holder, hodler);
                } else {
                    hodler = (ViewHolder) convertView.getTag(R.id.holder);
                }

                String item = (String) getItem(position);
                hodler.textView.setText(item);

                hodler.textView.setTag(R.id.item, item);
                hodler.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String item = (String) v.getTag(R.id.item);
                        onClickHistoryListener.onClickHistory(item);
                    }
                });

                break;
        }

        return convertView;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return getItemViewType(position) != TYPE_DATA_ITEM;
    }

    private class ViewHolder {
        TextView textView;
    }


    public interface OnClickHistoryListener {
        void onClickHistory(String item);
    }
}
